import { _decorator, Component, director, Input, input, Node, ResolutionPolicy, view } from 'cc';
import { toolsScript } from './toolsScript';
const { ccclass, property } = _decorator;

@ccclass('MainUI')
export class MainUI extends Component {
    private static instance: MainUI = null;

    onLoad() {
        this.initViewAdapter();
        if (MainUI.instance === null) 
        {
            MainUI.instance = this;
        } else {
            console.error("MainUI 已存在，请确保场景中只有一个 MainUI");
            this.node.destroy();
        }

        // input.on(Input.EventType.TOUCH_START,this.onTouchStart,this); //注册开始触摸事件
        // input.on(Input.EventType.TOUCH_END,this.onTouchEnd,this);//注册触摸结束事件

        // 读取字符串
        let topRecordString:string = toolsScript.getItem('topRecord');
        console.log("get value : ",topRecordString,"lengh : ",topRecordString.length)
        if(topRecordString != null && topRecordString.length > 0)
        {
        }
    }

    // 在游戏启动时调用
     initViewAdapter() {
        // 设置设计分辨率
        view.setDesignResolutionSize(1080, 1920, ResolutionPolicy.SHOW_ALL);
        
        // 根据实际屏幕比例动态调整
        const screenRatio = view.getVisibleSize().width / view.getVisibleSize().height;
        const designRatio = 1080 / 1920;
    
        if (screenRatio > designRatio) {
            // 宽屏设备（如 428×926），上下留黑边，宽度撑满
            view.setDesignResolutionSize(1080, 1920, ResolutionPolicy.FIXED_WIDTH);
        } else {
            // 窄屏设备，左右留黑边，高度撑满
            view.setDesignResolutionSize(1080, 1920, ResolutionPolicy.FIXED_HEIGHT);
        }
    }

    start() {

    }

    update(deltaTime: number) {
        
    }

    // 添加点击 重新开始游戏 事件 
    public onResStartButtonClick()
    {
        director.resume();

        director.loadScene("GameScenes"); 
        return;
    }

    //暂停游戏
    onPauseButtonClick()
    {
        director.pause();
    }

    //继续游戏
    onResumeButtonClick()
    {
        director.resume();
    }
}


